//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package event_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type EventInfoDbItem struct {
	EventId   string `bson:"_id"`
	EventData []byte `bson:"eventData"`
}

type _EventInfoDao struct {
	mc *mongo.Collection
}

func newEventInfoDao(db *mongo.Database) (*_EventInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_EVENT + "info")
	return &_EventInfoDao{mc: mc}, nil
}

func (db *_EventInfoDao) Insert(ctx context.Context, dbItem *EventInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_EventInfoDao) Get(ctx context.Context, eventId string) (*EventInfoDbItem, error) {
	retItem := &EventInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": eventId}).Decode(retItem)
	return retItem, err
}

func (db *_EventInfoDao) ListById(ctx context.Context, eventIdList []string) (map[string]*EventInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": eventIdList}})
	if err != nil {
		return nil, err
	}
	itemList := []*EventInfoDbItem{}
	err = cursor.All(ctx, &itemList)
	if err != nil {
		return nil, err
	}
	retMap := map[string]*EventInfoDbItem{}
	for _, item := range itemList {
		retMap[item.EventId] = item
	}
	return retMap, nil
}
